﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace QuickSynergy
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string config = GetConfig();
            string configPath = GetConfigPath();
            SaveConfig(config, configPath);
        }
        private string GetConfig()
        {
            string config = GenerateConfigFile(System.Windows.Forms.SystemInformation.ComputerName, txtLeft.Text, txtRight.Text, txtAbove.Text, txtBelow.Text);
            return config;
        }
        private static void SaveConfig(string config, string configPath)
        {
            using (StreamWriter sw = new StreamWriter(configPath))
            {
                sw.Write(config);
                MessageBox.Show("Configuration saved.  Right-click on Synergy Taskbar Icon and select \"Reload Configuration\"");
            }
        }
        private static string GetConfigPath()
        {
            string s = Environment.GetEnvironmentVariable("UserProfile");
            string configPath = Path.Combine(s, "Documents\\Synergy.sgc");
            return configPath;
        }
        private string GenerateConfigFile(string thisComputer, string left, string right, string above, string below)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("section: screens");

            if (string.IsNullOrEmpty(thisComputer) == true)
            {
                throw new Exception("You must have name for this computer");
            }

            sb.AppendLine("\t" + thisComputer + ":");
            sb.AppendLine("\t\tswitchCorners = none");
            sb.AppendLine("\t\tswitchCornerSize = 0");


            if (string.IsNullOrEmpty(left) == false && left.ToLower() != "left")
            {
                sb.AppendLine("\t" + left + ":");
                sb.AppendLine("\t\tswitchCorners = none");
                sb.AppendLine("\t\tswitchCornerSize = 0");
            }
            if (string.IsNullOrEmpty(right) == false && right.ToLower() != "right")
            {
                sb.AppendLine("\t" + right + ":");
                sb.AppendLine("\t\tswitchCorners = none");
                sb.AppendLine("\t\tswitchCornerSize = 0");
            }
            if (string.IsNullOrEmpty(above) == false && above.ToLower() != "above")
            {
                sb.AppendLine("\t" + above + ":");
                sb.AppendLine("\t\tswitchCorners = none");
                sb.AppendLine("\t\tswitchCornerSize = 0");
            }
            if (string.IsNullOrEmpty(below) == false && below.ToLower() != "below")
            {
                sb.AppendLine("\t" + below + ":");
                sb.AppendLine("\t\tswitchCorners = none");
                sb.AppendLine("\t\tswitchCornerSize = 0");
            }
            sb.AppendLine("end");

            sb.AppendLine("section: links");
            if (string.IsNullOrEmpty(left) == false && left.ToLower() != "left")
            {
                sb.AppendLine("\t" + left + ":");
                sb.AppendLine("\t\tright = " + thisComputer);
                sb.AppendLine("\t" + thisComputer + ":");
                sb.AppendLine("\t\tleft = " + left);
            }

            if (string.IsNullOrEmpty(right) == false && right.ToLower() != "right")
            {
                sb.AppendLine("\t" + thisComputer + ":");
                sb.AppendLine("\t\tright = " + right);
                sb.AppendLine("\t" + right + ":");
                sb.AppendLine("\t\tleft = " + thisComputer);
            }

            if (string.IsNullOrEmpty(above) == false && above.ToLower() != "above")
            {
                sb.AppendLine("\t" + above + ":");
                sb.AppendLine("\t\tdown = " + thisComputer);
                sb.AppendLine("\t" + thisComputer + ":");
                sb.AppendLine("\t\tup = " + above);
            }

            if (string.IsNullOrEmpty(below) == false && below.ToLower() != "below")
            {
                sb.AppendLine("\t" + below + ":");
                sb.AppendLine("\t\tup = " + thisComputer);
                sb.AppendLine("\t" + thisComputer + ":");
                sb.AppendLine("\t\tdown = " + below);
            }
            sb.AppendLine("end");

            sb.AppendLine("section: options");
            sb.AppendLine("end");
            return sb.ToString();

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = GetConfigPath();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                using (var sw = new StreamWriter(sfd.OpenFile()))
                {
                    sw.Write(GetConfig());
                }
            }            
        }
    }
}
